// Using Google Map to display the map
// Kenneth Ng, 12 Feb 2010
// filename: gmap.js

//******************************************************************
//Use Case 3.0 Show Map:  by Kenneth Ng,  lastupdated: 3/23/2010
//
//Review gmap.php for details 
//****************************************************************** 
var QUERY;
var ANS_Ary = new Array;

// Constants for control names
var NODE_ID_CurrentCty = "currentCountry";
var NODE_ID_MapAreaDiv = "map_canvas";


// Hide and Show Div
function divHide(divTagId) {
    var divTag = document.getElementById(divTagId);
    if (divTag != null) 
    {
        divTag.style.visibility = "hidden"; 
    }
}
function divReappear(divTagId) 
{
    var divTag = document.getElementById(divTagId);
    if (divTag != null) 
    {
        divTag.style.visibility = "visible"; 
    }
}
    
//Set the Click actions for buttons
function setButtonClickActions(id, callBack) {
    var objNode = document.getElementById(id);
    objNode.setAttribute("onclick", callBack);
}

//creates a new element node, appends it to parent.
// if text != "", this also creates a text node with text in it and appends it to element.
//sets the name and id attributes to nameId.
function addNode(parent, element, nameId, text) {
    var childEl = document.createElement(element);
    childEl.setAttribute("id", nameId);
    childEl.setAttribute("name", nameId);
    if (text != "") {
        textNode = document.createTextNode(text);
        childEl.appendChild(textNode);
    }
    parent.appendChild(childEl);
    return childEl;
}

// looks through an array of objects to find the object with 
// a particular attribute/value pair. Returns the object.
function findObject(array, attribute, value) {
    var targetObject = null;
    // find the object in array with attribute == value
    for (i = 0; i < array.length && targetObject == null; ++i) {
        if (eval("array[" + i + "]." + attribute) == value) {
            targetObject = array[i];
        }
    }
    return targetObject;
}

//Delete text from DIV node
function clearText(nameId) {
    // Clear the node when text exists
    var divNode = document.getElementById(nameId), child;
    while (child = divNode.firstChild) {
        divNode.removeChild(child);
    }
}
//Make empty on the text element 
function emptyText(nameId) {
    // Clear the node when text exists
    var textNode = document.getElementById(nameId);
    textNode.firstChild.nodeValue = '';    
}

// initialize map from google API
var map = null;
var geocoder = null;

// initialize for google map starts
function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById(NODE_ID_MapAreaDiv));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    map.setUIToDefault();
    geocoder = new GClientGeocoder();
  }
}

//Show the Map based on the address entered.  
//The address will be the country name that match with the data fill         
//You can optional use overlay address marker.openInfoWindowHtml(address);
function showAddress(address) {
    
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
           updateStatus(address + " not found! possible network issue.");
        } else {
           updateStatus();
           map.setCenter(point, 15);
           var marker = new GMarker(point);
           map.addOverlay(marker);
        }
      }
    );
  }
}

// Show the Map based on country Name.
// CountryName input is optional, it default to use the hidden input Current Country value
function showMap(CountryName){
    var currentCty;
    CountryName ? currentCty = CountryName : currentCty = document.getElementById(NODE_ID_CurrentCty).value;
    if(currentCty) showAddress(currentCty);
}

//update status Line in the bottom
function updateStatus (msg){
    var defaultMsg = "";
    var obj = document.getElementById("statusMsg");
    (msg) ? obj.firstChild.nodeValue = msg : obj.firstChild.nodeValue = defaultMsg;
}

window.onunload = function() {
    GUnload();
}
